package com.szholly.pro.watergas_server.ps.controller;


import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.TwoTuple;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.MessageException;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.BaseInfoService;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/BaseInfo/*")
public class BaseInfoController extends BaseController {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	BaseInfoService baseInfoService;

	/**
	 * 获取河流
	 * gislin
	 * 2016年8月15日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryRiver")
	public void queryRiver(HttpServletRequest resq, HttpServletResponse res) {
		String[] riverBasinCodes = RequestUnit.getStringArray("riverBasinCodes");
		List<RowBase> riverList;
		try {
			riverList = baseInfoService.queryRiver(riverBasinCodes);
			String resultJson = JsonUtil.ConvertArrayToJson(riverList);
			outString(resultJson);
		} catch (MessageException e) {
			outErrorString(e.getMessage());
		}
	}
	
	/**
	 * 获取流域
	 * gislin
	 * 2016年8月15日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryRiverBasin")
	public void queryRiverBasin(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		List<RowBase> riverList = baseInfoService.queryRiverBasin();
		String resultJson = JsonUtil.ConvertArrayToJson(riverList);
		outString(resultJson);
	}
	
	/**
	 * 获取人员角色信息
	 */
	@RequestMapping(value = "/queryUserRole")
	public void queryUserRole(HttpServletRequest resq, HttpServletResponse res){
		String userId = RequestUnit.getParameter("userId");
		String orgId = RequestUnit.getParameter("orgId");
		List<RowBase> orleList = baseInfoService.queryUserRole(orgId,userId);
		String resultJson = JsonUtil.ConvertArrayToJson(orleList);
		outString(resultJson);
	}
	
	/**
	 * 获取查询数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDistrict")
	public void getDistrict(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		TwoTuple<List<RowBase>, HashMap<String, List<RowBase>>> result = baseInfoService.getDistrict();
		
		JSONArray quJsonObject = JsonUtil.ConvertArrayToJsonObject(result.first);
		JSONObject jdJsobObject = JsonUtil.ConvertObjectToJsonObject(result.second);
		JSONObject resultObject = new JSONObject();
		resultObject.put("qu", quJsonObject);
		resultObject.put("jd", jdJsobObject);
		String jsonResult = JsonUtil.ConvertObjectToJson(resultObject);
		outString(jsonResult);
	}
	
}
